package cloud.dandanit.itservice.controller.banner;


import cloud.dandanit.itservice.entity.Banner;
import cloud.dandanit.itservice.service.BannerService;
import cloud.dandanit.itservice.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 * 幻灯片后台管理
 * @author luokai
 * @since 2022-06-19
 */
@Api("幻灯片后台管理")
//@CrossOrigin
@RestController
@RequestMapping("/itservice/banneradmin")
@SuppressWarnings({"AlibabaCommentsMustBeJavadocFormat", "unchecked"})
public class BannerAdminController {

    @Autowired
    private BannerService bannerService;

    /**
     * 后台获取前台显示的Banner列表
     * @return Banner列表
     */
    @ApiOperation(value = "获取所有Banner列表")
    @GetMapping("getIndexBannerList")
    public R getIndexBannerList() {

        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Banner::getState, 1)
                .orderByDesc(Banner::getSort)
                .last("limit 8");

        List<Banner> bannerList = bannerService.list(queryWrapper);
        return R.ok().data("list",bannerList);
    }

    @ApiOperation(value = "获取Banner分页列表")
    @GetMapping("pageBanner/{page}/{limit}")
    public R getPageBanner(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {

        Page<Banner> pageParam = new Page<>(page, limit);
        bannerService.page(pageParam,null);
        return R.ok().data("items", pageParam.getRecords()).data("total", pageParam.getTotal());
    }

    @ApiOperation(value = "新增Banner")
    @PostMapping("addBanner")
    public R addBanner(
            @ApiParam(name = "banner", value = "幻灯片实体", required = true)
            @RequestBody Banner banner) {

        bannerService.save(banner);
        return R.ok();
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("getBannerById/{id}")
    public R getBannerById(
            @ApiParam(name = "id", value = "幻灯片ID", required = true)
            @PathVariable String id) {

        Banner banner = bannerService.getById(id);
        return R.ok().data("item", banner);
    }

    @ApiOperation(value = "修改Banner")
    @PostMapping("updateBanner")
    public R updateBanner(
            @ApiParam(name = "banner", value = "幻灯片实体", required = true)
            @RequestBody Banner banner) {

        bannerService.updateById(banner);
        return R.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("removeBannerById/{id}")
    public R removeBannerById(
            @ApiParam(name = "id", value = "幻灯片ID", required = true)
            @PathVariable String id) {

        bannerService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "更改轮播图状态")
    @GetMapping("changeState/{bannerId}")
    public R changeState(
            @ApiParam(name = "bannerId", value = "轮播图ID", required = true)
            @PathVariable String bannerId) {

        Banner banner = bannerService.getById(bannerId);
        if (banner.getState()==0) {
            banner.setState(1);
        } else {
            banner.setState(0);
        }
        bannerService.updateById(banner);

        return R.ok();
    }

    @ApiOperation(value = "一键修改轮播图状态")
    @GetMapping("batchChangeBannerState/{state}")
    public R batchChangeBannerState(
            @ApiParam(name = "state", value = "更改的状态", required = true)
            @PathVariable Integer state) {

        List<Banner> bannerList = bannerService.list(null);
        for (Banner banner : bannerList) {
            banner.setState(state);
        }
        bannerService.updateBatchById(bannerList);
        return R.ok();
    }
}

